package org.springframework.amqp.tutorials.config.topic;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TopicRabbitMqConfig {

    private final static String student = "people.student";
    private final static String teacher = "people.teacher";

    @Bean
    TopicExchange myTopicExchange() {
        return new TopicExchange("myTopicExchange", true, false);
    }

    /**
     * people.student 队列
     */
    @Bean
    Queue myQueueOfStudent() {
        return new Queue(TopicRabbitMqConfig.student);
    }

    /**
     * people.teacher 队列
     */
    @Bean
    Queue myQueueOfTeacher() {
        return new Queue(TopicRabbitMqConfig.teacher);
    }

    /**
     * people 队列
     */
    @Bean
    Queue myQueueOfPeople() {
        return new Queue("people");
    }

    /**
     * myBindingofStudent 只能收到路由键为routing key==people.student的消息
     */
    @Bean
    Binding myBindingofStudent() {
        return BindingBuilder.bind(myQueueOfStudent()).to(myTopicExchange()).with(TopicRabbitMqConfig.student);
    }

    /**
     * myBindingofteacher 只能收到路由键为routing key == people.teacher的消息
     */
    @Bean
    Binding myBindingOfTeacher() {
        return BindingBuilder.bind(myQueueOfTeacher()).to(myTopicExchange()).with(TopicRabbitMqConfig.teacher);
    }

    /**
     * myBingingofPeople 由于routing key == people.# 所以能够收到以上队列的所有消息
     */
    @Bean
    Binding myBindingPeople() {
        return BindingBuilder.bind(myQueueOfPeople()).to(myTopicExchange()).with("people.#");
    }

}